package com.yusys.jin.blog.controller;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;


import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.yusys.jin.blog.entity.commonEntity.District;
import com.yusys.jin.blog.entity.commonEntity.ServerArea;
import com.yusys.jin.blog.entity.commonEntity.ServerServer;
import com.yusys.jin.blog.ex.SendTooFastException;
import com.yusys.jin.blog.serviceimpl.CommonServiceimpl;
import com.yusys.jin.blog.util.ResponseResult;
import com.yusys.jin.blog.util.VerifiedCode;

/**
 * 公共功能控制类
 *
 * @author LENOVO
 */

@RestController
@RequestMapping("/public")
public class Controller extends BaseController {

    @Autowired
    CommonServiceimpl service;


    private Map<Integer, Color> colors = VerifiedCode.getColors();
    char[] letters = VerifiedCode.getLetters();

    @RequestMapping("/getverifiedcode")
    @ResponseBody
    public void getCode(HttpSession session, HttpServletResponse response) {
        BufferedImage image = new BufferedImage(70, 35, BufferedImage.TYPE_INT_RGB);   //生成图片
        Graphics g = image.getGraphics();

        Color c = new Color(114, 163, 228);

        g.setColor(c);
        g.fillRect(0, 0, 70, 35);
        g.setFont(new Font("华文行楷", Font.HANGING_BASELINE, 26));
        Random rand = new Random();

        String str = getCode();
        g.setColor(colors.get(rand.nextInt(11)));
        g.drawString(str, 0, 30);
        //给图片画干扰线
        Graphics2D g2d = (Graphics2D) g;
        g2d.setStroke(new BasicStroke(0.6f));//加粗画笔
        for (int i = 0; i < 3; i++) {
            int x1 = rand.nextInt(70);
            int x2 = rand.nextInt(70);
            int y1 = rand.nextInt(35);
            int y2 = rand.nextInt(35);
            int choose = rand.nextInt(11);
            g2d.setColor(colors.get(choose));
            g2d.drawLine(x1, y1, x2, y2);
        }
        str = str.toUpperCase();

        response.setHeader("Content-Type", "image/jpeg");
        session.removeAttribute("code");
        session.setAttribute("code", str);  //将验证码保存在session中
        response.setContentType("image/jpeg");//设置相应类型,告诉浏览器输出的内容为图片
        response.setHeader("Pragma", "No-cache");//设置响应头信息，告诉浏览器不要缓存此内容
        response.setHeader("Cache-Control", "no-cache");
        response.setDateHeader("Expire", 0);
        try {
            ServletOutputStream out = response.getOutputStream();
            ImageIO.write(image, "png", out);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    //发送邮件验证码
    @RequestMapping("getmailcode")
    public ResponseResult<Void> sendmailcode(String email, HttpSession session) throws SendTooFastException {
        System.err.println("邮件请求发送成功");

        service.getMailCode(email, session);
        ResponseResult<Void> rr = new ResponseResult<Void>();
        rr.setState(200);
        rr.setMessage("发送成功");
        return rr;
    }

    //查询魔兽大区名称
    @GetMapping("getServerAreas")
    public ResponseResult<List<ServerArea>> getServerArea() {
        System.err.println("查询大区请求发送成功");
        ResponseResult<List<ServerArea>> rr = new ResponseResult<List<ServerArea>>();
        List<ServerArea> list = service.getAreas();
        rr.setData(list);
        rr.setState(200);
        rr.setMessage("获取成功");
        return rr;
    }

    //查询服务器名称
    @GetMapping("getServers")
    public ResponseResult<List<ServerServer>> getservers(Integer areaId) {
        System.err.println("查询服务器请求发送成功");
        System.out.println(areaId);
        ResponseResult<List<ServerServer>> rr = new ResponseResult<List<ServerServer>>();
        List<ServerServer> list = service.getServers(areaId);
        rr.setData(list);
        rr.setState(200);
        rr.setMessage("获取成功");
        return rr;
    }

    //查询省市区
    @GetMapping("getDistrict")
    public ResponseResult<List<District>> getDistrict(Integer parentId) {
        System.err.println("查询省市区请求发送成功");
        ResponseResult<List<District>> rr = new ResponseResult<List<District>>();
        rr.setData(service.getDistrict(parentId));
        rr.setMessage("获取成功");
        rr.setState(200);
        return rr;
    }

    @PostMapping("getMenu")
    public ResponseResult<Map<Integer, String>> getMenu(@RequestParam MultiValueMap<String, Object> params) {
        ResponseResult<Map<Integer, String>> rr = new ResponseResult<Map<Integer, String>>();
        Map<String, List<Object>> testmap = new HashMap<String, List<Object>>();
        testmap = params;
        List<Object> list = testmap.get("state");
        Integer state = Integer.parseInt((String) list.get(0));

        System.err.println("id是" + state);
        Map<Integer, String> map = service.getMenus(state);
        System.out.println(map);
        rr.setData(map);
        rr.setMessage("获取成功");
        rr.setState(200);


        return rr;
    }

    private String getCode() {
        Random rand = new Random();
        StringBuilder builder = new StringBuilder();
        //生成随机验证码
        for (int i = 0; i < 4; i++) {
            int num = rand.nextInt(62);
            builder.append(letters[num]);
        }
        String str = builder.toString().trim();
        return str;
    }
}
